package com.whfc.iot.fse.crane.codec;

import com.whfc.common.util.MathUtil;
import com.whfc.iot.fse.crane.entity.TwMsgWorkCycle;
import com.whfc.iot.util.MsgDataCodecUtil;
import io.netty.buffer.ByteBuf;

import java.util.Date;

public class TwMsgDataCodecWorkCycle implements TwMsgDataCodec<TwMsgWorkCycle>{
    @Override
    public void decode(ByteBuf buf, TwMsgWorkCycle msg) {
        // 版本号
        int version = buf.readUnsignedByte();
        // 厂家及设备类型
        int equipmentType = buf.readUnsignedByte();
        // 开始时间（6byte年月日时分秒)
        Date startTime = MsgDataCodecUtil.readDateTime(buf);
        // 结束时间（6byte年月日时分秒)
        Date endTime = MsgDataCodecUtil.readDateTime(buf);
        // 吊重
        int maxWeight = buf.readUnsignedShort();
        // 力矩
        int maxMoment = buf.readUnsignedShort();
        // 最大高度
        int maxHeight = buf.readUnsignedShort();
        // 最小高度
        int minHeight = buf.readUnsignedShort();
        // 最大幅度
        int maxMargin = buf.readUnsignedShort();
        // 最大幅度
        int minMargin = buf.readUnsignedShort();
        // 起吊点角度
        int startHangAngle = buf.readUnsignedShort();
        // 起吊点幅度
        int startHangMargin = buf.readUnsignedShort();
        // 起吊点高度
        int startHangHeight = buf.readUnsignedShort();
        // 卸吊点角度
        int endHangAngle = buf.readUnsignedShort();
        // 卸吊点幅度
        int endHangMargin = buf.readUnsignedShort();
        // 卸吊点高度
        int endHangHeight = buf.readUnsignedShort();

        msg.setVersion(version);
        msg.setEquipmentType(equipmentType);
        msg.setStartTime(startTime);
        msg.setEndTime(endTime);
        msg.setMaxWeight(MathUtil.round(0.001*maxWeight,3));
        msg.setMaxMoment(MathUtil.round(0.01*maxMoment,2));
        msg.setMaxHeight(MathUtil.round(0.1*maxHeight,1));
        msg.setMinHeight(MathUtil.round(0.1*minHeight,1));
        msg.setMaxMargin(MathUtil.round(0.1*maxMargin,1));
        msg.setMinMargin(MathUtil.round(0.1*minMargin,1));
        msg.setStartHangAngle(MathUtil.round(0.1*startHangAngle,1));
        msg.setStartHangMargin(MathUtil.round(0.1*startHangMargin,1));
        msg.setStartHangHeight(MathUtil.round(0.1*startHangHeight,1));
        msg.setEndHangAngle(MathUtil.round(0.1*endHangAngle,1));
        msg.setEndHangMargin(MathUtil.round(0.1*endHangMargin,1));
        msg.setEndHangHeight(MathUtil.round(0.1*endHangHeight,1));
    }
}
